<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="消息类型" prop="messageType">
        <el-select v-model="queryParams.messageType" placeholder="请选择消息类型" clearable size="small">
          <el-option v-for="dict in messageTypeOptions" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue" />
        </el-select>
      </el-form-item>
      <el-form-item label="消息内容" prop="message">
        <el-input v-model="queryParams.message" placeholder="请输入消息内容" clearabl size="small" @keyup.enter.native="handleQuery" />
      </el-form-item>
<!--      <el-form-item label="是否查看" prop="isView">
        <el-select v-model="queryParams.isView" placeholder="请选择是否查看" clearable size="small">
          <el-option v-for="dict in isViewOptions" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue" />
        </el-select>
      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!--<el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"

        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"

        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:info:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:info:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>-->

    <el-table v-loading="loading" :data="infoList" @selection-change="handleSelectionChange" @row-dblclick='RowDbclick'>
      <el-table-column type="selection" width="55" align="center" />
      <!--     <el-table-column label="id" align="center" prop="id" />-->
      <!--      <el-table-column label="名称" align="center" prop="name" />
      <el-table-column label="编号" align="center" prop="code" />-->
      <el-table-column label="消息类型" align="center" prop="messageType" :formatter="messageTypeFormat" />
      <el-table-column label="消息内容" align="center" prop="message" />
      <el-table-column label="菜单名称" align="center" prop="menoName" />
      <!-- <el-table-column label="是否查看" align="center" prop="isView" :formatter="isViewFormat" />-->
      <!--      <el-table-column label="备注" align="center" prop="remark" />-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-view" @click="handleView(scope.row)">查看</el-button>

        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

    <!-- 添加或修改消息信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="90px">
        <el-row>
          <!--<el-col :span="12">
            <el-form-item label="名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入名称" :disabled="openDisabled" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="编号" prop="code">
              <el-input v-model="form.code" placeholder="请输入编号" :disabled="openDisabled" />
            </el-form-item>
          </el-col>-->
          <el-col :span="12">
            <el-form-item label="消息类型" prop="messageType">
              <el-select v-model="form.messageType" placeholder="" :disabled="openDisabled">
                <el-option v-for="dict in messageTypeOptions" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <!-- <el-col :span="12">
            <el-form-item label="消息内容" prop="message">
              <el-input v-model="form.message" placeholder="" :disabled="openDisabled" />
            </el-form-item>
          </el-col>-->
          <!--          <el-col :span="12">
            <el-form-item label="菜单名称" prop="menoName">
              <el-input v-model="form.menoName" placeholder="" :disabled="openDisabled" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="菜单路径" prop="menoUrl">
              <el-input v-model="form.menoUrl" placeholder="" :disabled="openDisabled" />
            </el-form-item>
          </el-col>-->
          <!--<el-col :span="12">
            <el-form-item label="是否查看" prop="isView">
              <el-select v-model="form.isView" placeholder="请选择是否查看" :disabled="openDisabled">
                <el-option
                  v-for="dict in isViewOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>-->
          <!-- <el-col :span="12">
            <el-form-item label="信息配置id" prop="mconfigId">
              <el-input v-model="form.mconfigId" placeholder="请输入信息配置id" :disabled="openDisabled" />
            </el-form-item>
          </el-col>-->
          <el-col :span="24">
            <el-form-item label="消息内容" prop="message">
              <el-input v-model="form.message" type="textarea" placeholder="请输入内容" :disabled="openDisabled" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer" v-if="!openDisabled">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  getUserNum,
  listInfoU,
  getInfo,
  delInfo,
  addInfo,
  updateInfo,
} from "@/api/basic/message/info";

export default {
  name: "Info",
  data() {
    return {
      //弹窗是否禁止编辑
      openDisabled: true,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 消息信息表格数据
      infoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 消息类型字典
      messageTypeOptions: [],
      // 是否查看字典
      isViewOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        messageType: null,
        message: null,
        isView: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
    };
  },
  beforeRouteEnter(to, from, next) {
    next((vm) => {
      if (from.path.indexOf("/basic") == -1) {
        vm.$store.state.app.sidebar.opened = false;
        vm.$store.state.permission.sidebarRouters = [];
      }
      // vm.pathUrl = from.fullPath;
    });
  },
  created() {
    this.getList();
    this.getDicts("basic_message_type").then((response) => {
      this.messageTypeOptions = response.data;
    });
    this.getDicts("yes_no").then((response) => {
      this.isViewOptions = response.data;
    });
    // let router = vm.$store.state.permission.routes;
    // for (let item of router) {
    //   if (item.path == "/basic") {
    //     this.$store.state.permission.sidebarRouters = item.children;
    //   }
    // }
  },
  methods: {
    RowDbclick(row, column, event) {
      this.$router.push(row.menoUrl);
    },
    /** 根据角色ID查询菜单树结构 */
    getUserNumber() {
      getUserNum().then((response) => {
        var num = response.number;
      });
    },
    /** 查询消息信息列表 */
    getList() {
      this.loading = true;
      listInfoU(this.queryParams).then((response) => {
        this.infoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 消息类型字典翻译
    messageTypeFormat(row, column) {
      return this.selectDictLabel(this.messageTypeOptions, row.messageType);
    },
    // 是否查看字典翻译
    isViewFormat(row, column) {
      return this.selectDictLabel(this.isViewOptions, row.isView);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        code: null,
        messageType: null,
        message: null,
        menoName: null,
        menoUrl: null,
        isView: null,
        mconfigId: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        delFlag: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.openDisabled = false;
      this.open = true;
      this.title = "添加消息信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.openDisabled = false;
      const id = row.id || this.ids;
      getInfo(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改消息信息";
      });
    },
    /** 查看按钮 */
    handleView(row) {
      this.$router.push(row.menoUrl);
/*      this.reset();
      this.openDisabled = true;
      const id = row.id || this.ids;
      getInfo(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "查看消息信息";
      });*/
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateInfo(this.form).then((response) => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            const rLoading = this.openLoading();
            addInfo(this.form)
              .then((response) => {
                this.msgSuccess("新增成功");
                this.open = false;
                rLoading.close();
                this.getList();
              })
              .catch((error) => {
                rLoading.close();
              });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm(
        '是否确认删除消息信息编号为"' + ids + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return delInfo(ids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/info/export",
        {
          ...this.queryParams,
        },
        "消息信息" + this.parseTime(new Date()) + ".xlsx"
      );
    },
  },
};
</script>
<style scoped>
</style>
